﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace OnlineBusTicket
{
    public partial class Search : System.Web.UI.Page
    {
        public static string SDeparturePlace;
        public static string SArrivePlace;
        public static DateTime SDate;
        public static string BusListID;
        public static string BusCatelogy;
        public static string Departure;
        public static string Arrive;
        public static string StartDate;
        public static string EndDate;
        public static string Time;
        public static string Price;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
                con.Open();
                SqlDataAdapter da1 = new SqlDataAdapter("Select distinct [DeparturePlace] FROM [BusList]",con);
                SqlDataAdapter da2 = new SqlDataAdapter("Select distinct [ArrivePlace] FROM [BusList]", con);
                DataSet ds1 = new DataSet();
                DataSet ds2 = new DataSet();
                da1.Fill(ds1);
                da2.Fill(ds2);
                ddlDeparturePlace.DataSource = ds1;
                ddlDeparturePlace.DataTextField = "DeparturePlace";
                
                ddlDeparturePlace.DataBind();
                ddlArrivePlace.DataSource = ds2;
                ddlArrivePlace.DataTextField = "ArrivePlace";
                
                ddlArrivePlace.DataBind();
            }

        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            SDeparturePlace = ddlDeparturePlace.Text;
            SArrivePlace = ddlArrivePlace.Text;
            SDate = Convert.ToDateTime(txtDate.Text);
            OnlineBusTicket.Component.BusListQuery obj=new Component.BusListQuery();
            if (obj.GetBusList() != null)
            {
                dgvSearch.DataSource = obj.GetBusList();
                dgvSearch.DataBind();
            }
            else
            {
                Label4.Text = "Could not found";
            }


        }
        protected void dgvSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = dgvSearch.SelectedRow;
            BusListID = row.Cells[1].Text;
            BusCatelogy = row.Cells[2].Text;
            Departure = row.Cells[3].Text;
            Arrive = row.Cells[4].Text;
            StartDate = row.Cells[5].Text;
            EndDate = row.Cells[6].Text;
            Time = row.Cells[7].Text;
            Price = row.Cells[8].Text;
           
            MembershipUser u = Membership.GetUser(HttpContext.Current.User.Identity.Name);
            if (u != null)
            {
                
                Response.Redirect("~/BookTicket.aspx");
            }
            else
            {
                MessageBox.Show("You Must Login", "Warning", MessageBoxButtons.OK);
            }
        }
    }
}